﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Host
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                ServiceHost servicebusHost = new ServiceHost(typeof(ServiceBusLibrary.ServiceBus));
                servicebusHost.Open();
                Console.WriteLine("Service Bus is up and running with the folowing endpoints");
                foreach (var endpoint in servicebusHost.Description.Endpoints)
                {
                    Console.WriteLine(endpoint.Address.ToString());
                }

                ServiceHost authorHost = new ServiceHost(typeof(AuthorServiceLibrary.AuthorService));
                authorHost.Open();
                Console.WriteLine("Author service is up and running with the folowing endpoints");
                foreach (var endpoint in authorHost.Description.Endpoints)
                {
                    Console.WriteLine(endpoint.Address.ToString());
                }

                ServiceHost coreHost = new ServiceHost(typeof(CoreServiceLibrary.CoreService));
                coreHost.Open();
                Console.WriteLine("Core service is up and running with the folowing endpoints");
                foreach (var endpoint in coreHost.Description.Endpoints)
                {
                    Console.WriteLine(endpoint.Address.ToString());
                }

                ServiceHost editorHost = new ServiceHost(typeof(EditorServiceLibrary.EditorService));
                editorHost.Open();
                Console.WriteLine("Editor service is up and running with the folowing endpoints");
                foreach (var endpoint in editorHost.Description.Endpoints)
                {
                    Console.WriteLine(endpoint.Address.ToString());
                }

                ServiceHost reviewerHost = new ServiceHost(typeof(ReviewerServiceLibrary.ReviewerService));
                reviewerHost.Open();
                Console.WriteLine("Reviewer service is up and running with the folowing endpoints");
                foreach (var endpoint in reviewerHost.Description.Endpoints)
                {
                    Console.WriteLine(endpoint.Address.ToString());
                }

                ServiceHost publishHost = new ServiceHost(typeof(PublishingEditorServiceLibrary.PublishingEditorService));
                publishHost.Open();
                Console.WriteLine("Publishing Editor service is up and running with the folowing endpoints");
                foreach (var endpoint in publishHost.Description.Endpoints)
                {
                    Console.WriteLine(endpoint.Address.ToString());
                }

                Console.WriteLine("Press ENTER to stop HSE PS services...");
                
                Console.ReadLine();
                reviewerHost.Close();
                servicebusHost.Close();
                authorHost.Close();
                coreHost.Close();
                editorHost.Close();
                publishHost.Close();

                authorHost.Abort();
                coreHost.Abort();
                editorHost.Abort();
                reviewerHost.Abort();
                publishHost.Abort();
                servicebusHost.Abort();

                Console.WriteLine("All services have been shut down");
                
            }
            catch (Exception ex)
            {
                Console.WriteLine("Details: " + ex.Message);
                if (ex.InnerException != null)
                {
                    ex = ex.InnerException;
                    Console.WriteLine(ex.Message);
                }
            }
            finally
            {
                Console.WriteLine("Application stopped");
                Console.ReadLine();
            }
        }
    }
}
